package xin.marcher.labs.io.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;

public class NioSelectorServer {

    public static void main(String[] args) throws IOException {
        // 创建 NIO ServerSocketChannel
        ServerSocketChannel serverSocket = ServerSocketChannel.open();
        serverSocket.socket().bind(new InetSocketAddress(9000));
        // 设置 ServerSocketChannel 为非阻塞
        serverSocket.configureBlocking(false);
        // 打开 Selector 处理 Channel, 即创建 epoll
        Selector selector = Selector.open();
        // 把 ServerSocketChannel 注册到 selector 上, 并且 selector 对客户端 accept 连接操作感兴趣
        SelectionKey selectionKey = serverSocket.register(selector, SelectionKey.OP_ACCEPT);
        System.out.println("服务启动成功...");

        while (true) {
            // 阻塞等待需要处理的事件发生
            selector.select();

            // 获取 selector 中注册的全部事件的 SelectionKey
            Set<SelectionKey> selectionKeys = selector.selectedKeys();
            Iterator<SelectionKey> iterator = selectionKeys.iterator();

            // 遍历SelectionKey对事件进行处理
            while (iterator.hasNext()) {
                SelectionKey key = iterator.next();
                // 如果是 OP_ACCEPT 事件, 则进行连接截取和事件注册
                if (key.isAcceptable()) {
                    ServerSocketChannel server = (ServerSocketChannel) key.channel();
                    SocketChannel socketChannel = server.accept();
                    socketChannel.configureBlocking(false);
                    // 这里只注册了读事件, 如果需要给客户端发送数据可以注册写事件
                    SelectionKey selKey = socketChannel.register(selector, SelectionKey.OP_READ);
                    System.out.println("客户端连接成功...");
                }
                // 如果是 OP_READ 事件, 则进行读取和打印
                else if (key.isReadable()) {
                    SocketChannel socketChannel = (SocketChannel) key.channel();
                    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(128);
                    int len = socketChannel.read(byteBuffer);
                    // 有数据
                    if (len > 0) {
                        System.out.println("接收到消息: " + new String(byteBuffer.array()));
                    } else if (len == -1) {
                        System.out.println("客户端断开连接...");
                        socketChannel.close();
                    }
                }
            }
            // 从事件集合里删除本次处理的 key, 防止下次 select 重复处理
            iterator.remove();
        }

    }
}
